package com.cupster.func_db_greendao.crud;

import android.content.Context;
import android.util.Log;

import com.cupster.func_db_greendao.DaoManager;
import com.cupster.func_db_greendao.auto.UserDao;
import com.cupster.func_db_greendao.entity.User;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class UserDaoHelper {

    private static final String TAG = "UserDaoHelper";
    private DaoManager mManager;

    public UserDaoHelper(Context appContext){
        mManager = DaoManager.getInstance( appContext );
    }

    /**
     * 单条插入
     * @param user
     * @return
     */
    public boolean insert(User user){
        boolean result = mManager.getDaoSession().getUserDao().insert(user) != -1;
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "insert: " + result +" ==> "+ user.toString());
        }
        return result;
    }

    /**
     * 多条item插入
     * @param userList
     * @return boolean结果
     */
    public boolean insertMulti(final List<User> userList){
        boolean result = false;
        try {
            mManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (User user : userList){
                       long add_id = mManager.getDaoSession().insertOrReplace(user) ;
                        if (DaoManager.SHOW_LOG){
                            Log.d(TAG, "insert items : " + add_id +" ==> "+ user.toString());
                        }
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }


    public boolean update(User user){
        boolean result = false;
        try {
            mManager.getDaoSession().update(user);
            result = true;
        }catch (Exception e){
            e.printStackTrace();
        }
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "update: " + result +" ==> "+ user.toString());
        }
        return result;
    }


    public boolean delete(User user){
        boolean result = false;
        try {
            mManager.getDaoSession().delete(user);
            result = true;
        }catch (Exception e ){e.printStackTrace();}
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "delete: " + result +" ==> "+ user.toString());
        }
        return result;
    }


    public boolean deleteAll(){
        boolean result = false;
        try {
            mManager.getDaoSession().deleteAll(User.class);
            result = true;
        }catch (Exception e ){
            e.printStackTrace();
        }
        if (DaoManager.SHOW_LOG){
            Log.d(TAG, "delete all : " + result );
        }
        return result;
    }

    public  List <User> queryAll (){
        return mManager.getDaoSession().loadAll(User.class);
    }

    public User queryById(long _id){
        return mManager.getDaoSession().load(User.class ,_id);
    }

    public List<User> queryBySQL(String sql , String[] conditions){
        return mManager.getDaoSession().queryRaw(User.class ,sql ,conditions);
    }

    /**
     * 使用queryBuilder 条件查询
     * @return
     */
    public List<User> queryByQueryBuilder(long id){
        QueryBuilder<User> queryBuilder = mManager.getDaoSession().queryBuilder(User.class);
        return queryBuilder.where(UserDao.Properties._id.eq(id)).list();
    }
    /**
     * 使用queryBuilder 条件查询
     * @return
     */
    public List<User> queryByQueryBuilder(String account){
        QueryBuilder<User> queryBuilder = mManager.getDaoSession().queryBuilder(User.class);
        return queryBuilder.where(UserDao.Properties.Account.eq(account)).list();
    }

    /**
     *
     * @param isDelete 0=false 1=true
     * @return
     */
    public List<User> queryByQueryBuilder(int isDelete){
        QueryBuilder<User> queryBuilder = mManager.getDaoSession().queryBuilder(User.class);
        return queryBuilder.where(UserDao.Properties.Dr.eq(isDelete)).list();
    }

}
